###三创购物系统###
###author:hxli
####data：11-1
import time
user_dic = {'hxli':'520','xuxu':'1314',"wen":0} # hxli用户为一般用户，xuxu用户为管理员用户：用来添加货物种类和修改货物价格。
commodity_list = {'F00001':{'name':'苹果','price':1.2},'F00002':{'name':'香蕉','price':5.5},'F00003':{'name':'哈密瓜','price':6.0}}
shopping_trolley = {None:'购物车空空如也呢'}    # 购物车商品和总价字典
shopping_trolley1 = {None:'None'}  # 购物车数量字典
balance = 50
have_bought = {None:'还没有购买过东西哦'}
print("############欢迎使用三创购物系统#############")
def register(): # 登录功能
    while True:
        username = input("请输入需要登录的用户名：")
        i = user_dic.get(username, 0)
        if username in user_dic:
            password = input(f"请输入用户{username}的密码：")
            if password == i:  # 判断密码的吻合
                print("登录成功！")
                if username == 'xuxu' :
                    return dba()
                else:
                    return choose()
            else:
                print("密码错误！")
                choose1 = input("请选择按enter继续登录或者输入quit退出")
                if choose1 == "quit":  # 判断退出
                    exit()
                else:
                    pass
        else:
            print("用户名不存在！")
            choose1 = input("请选择按enter继续登录或者输入quit退出或者输入1进行新用户注册：")
            if choose1 == "quit":  # 判断退出
                exit()
            elif choose1 == '1':
                return login()
            else:
                pass
def login(): # 注册功能
    a = input("请输入你要注册的用户名：")
    if a  not in user_dic:
        b = input(f"请输入用户{a}所要设置的密码：")
        user_dic[a] = b
        print("注册成功，将在5秒后进入登录页面")
        time.sleep(5)
        return register()
    else :
        print("用户名已存在，无法重复注册")
        time.sleep(1)
        return login()
def choose():   # 菜单功能
    choose = input("欢迎光临三创红浪漫!\n你可以选择：\n1,查看商品并购买\n2,查看购物车\n3,查看余额\n4,充值\n5,退出\n在此输入你的选择：")
    if choose == '1' :
        commodity()
    elif choose == '2':
        trolley()
    elif choose == '3':
        balance1()
    elif choose == '4':
        recharge()
    elif choose == '5':
        exit1()
    else:
        print("不要输入不相关的东西")
        return choose
def commodity(): # 选择商品加入到购物车功能
    global a
    #commodity_list = {'F00001':{'name':'苹果','price':1.2},'F00002':{'name':'香蕉','price':5.5},'F00003':{'name':'哈密瓜','price':6.0}}
    for i in commodity_list:
        print(f"商品编号{i},商品名称：{commodity_list.get(i).get('name')},商品价格：{commodity_list.get(i).get('price')}")
    commodity_choose = input("请输入你要选择的商品编号：")
    if commodity_choose in commodity_list:
        name1 = str(commodity_list.get(commodity_choose).get('name'))
        print(f"你选择的是{name1},")
        commodity_choose_num = int(input(f"请输入你要购买{name1}的数量"))
        commodity_price = commodity_choose_num * commodity_list.get(commodity_choose).get('price')  # 商品总价
        shopping_trolley1[name1] = commodity_choose_num # 购物车数量字典添加
        shopping_trolley[name1] =  commodity_price  # 购物车价格字典添加
        #shopping_trolley1 = {"苹果":2}
        #shopping_trolley = {"苹果":2.4}
        shopping_trolley.pop(None,None)  # 删除原本字典里的东西
        shopping_trolley1.pop(None, None) #删除原本字典的东西
        print("商品已经进入购物车")
        a = input("请选择继续购买或者返回到主页面或者退出(按enter继续购买，输入1返回首页，输入exit退出):")
        return judge()
    else:
        a = input("此商品不存在，请重新输入,输入1返回首页，输入exit退出")
        return judge()
def judge(): #小型判断功能
    if a == '1':
        return choose()
    elif a == 'exit':
        return exit1()
    else:
        return commodity()
def trolley(): #购物车功能
    global a
    for i in shopping_trolley:
        if i == None :
            print(f"{shopping_trolley.get(i)}")
            print("将在5秒后自动返回首页")
            time.sleep(5)
            return choose()
        else:
            print(f"{i}，数量：{shopping_trolley1.get(i)},总价：{shopping_trolley.get(i)}元")
    b = input("如果想结算，请输入1，如果不想结算请输入任意值：")
    if b == '1':
        return clear1()
    else:
        a = input("请输入1返回首页或者输入exit退出")
        if a == '1':
            return choose()
        elif a == 'exit':
            return exit1()
        else:
            print("请不要输入不相关的东西")
            return trolley()
def clear1():  # 结算
    global balance
    clear = input("请选择输入要结算的商品")
    if clear in shopping_trolley:
        clear_num = int(input("请输入要结算的商品数量"))
        if clear_num <= shopping_trolley1.get(clear):
            clear_price = clear_num * shopping_trolley.get(clear,None) / shopping_trolley1.get(clear,None)
            if clear_price <= balance:
                balance -= clear_price
                have_bought.pop(None,None)
                have_bought[clear] = clear_num
                shopping_trolley1[clear] = shopping_trolley1.get(clear) - clear_num
                if shopping_trolley1.get(clear) == 0 :  #当商品数量为0时，使商品不再存在于表单
                    shopping_trolley1.pop(clear,None)
                    print("结算成功！,将在3秒后自动返回首页")
                    time.sleep(3)
                    return choose()
                else:
                    print("结算成功！,将在3秒后自动返回首页")
                    time.sleep(3)
                    return choose()
            else:
                c = input("您的余额不足，请输入充值选择充值或者按enter重新结算或者输入1返回首页或者输入exit退出")
                if c == '充值':
                    return recharge()
                elif c == '1':
                    return choose()
                elif c == 'exit' :
                    return exit1()
                else:
                    return clear1()
        else:
            d = input("你的购物车所添加的数量不够，请按1返回首页或者按2更改数量或者按enter重新选择或者输入exit退出：")
            if d == '1':
                return choose()
            elif d == '2':
                e = int(input("请选择你要修改的数量："))
                shopping_trolley1[clear] = e
                shopping_trolley[clear] = commodity_list.get(clear).get('price') * e
                print("修改成功")
                return clear1()
            elif d == 'exit':
                return exit1()
            else:
                return clear1()
    else:
        f = input("您的购物车里不含有此类商品，请按enter重新输入或者按1回到首页或者输入exit退出")
        if f == '1':
            return choose()
        elif f == 'exit':
            return exit1()
        else:
            return clear1()
def balance1(): #查看余额功能
    global balance
    print(f"您的当前余额为{balance}")
    a = input("请输入1返回首页或者输入exit退出")
    if a == '1':
        return choose()
    elif a == 'exit':
        exit1()
    else:
        print("请不要输入不相关的东西")
        return balance1()
def recharge(): #充值功能
    global balance
    w = input("需要充值请按1，误触想返回首页请按2，想直接退出请按3")
    if w == '1':
        q = float(input("请输入你要充值的金额："))
        balance += q
        print("充值成功，3秒后自动返回首页")
        time.sleep(3)
        return choose()
    elif w == '2':
        return choose()
    elif w == '3':
        exit1()
    else:
        return recharge()
def exit1(): #退出打印功能
    print("退出！")
    for i in have_bought:
        print(f"您已购买{i},数量：{have_bought.get(i)}")
    print(f"您的余额：{balance},欢迎下次光临！三创红浪漫永远欢迎您！")
    exit()
def dba():
    print(f"欢迎进入货物修改界面，欢迎你管理员xuxu\n,当前货物和价格如下：")
    for i in commodity_list:
        print(f"商品编号{i},商品名称：{commodity_list.get(i).get('name')},商品价格：{commodity_list.get(i).get('price')}")
    numb = input("如要新添，请输入1;如要修改，请输入2;如要删除，请输入3;如要退出，请输入exit。")
    if  numb == '1':
        a = input("请输入你要添加的商品编号：（请务必不要冲突）")
        if a in commodity_list:
            print("请不要输入已有编号，请重试")
            return dba()
        else:
            b = input("请输入商品名称：")
            c = input("请输入商品价格：")
            commodity_list[a] = {'name':b,'price':c}
            print("添加成功")
            return dba()
    elif numb == '3':
        a = input("请输入你要删除的商品编号：")
        if a in commodity_list:
            commodity_list.pop(a,None)
            print("删除成功")
            return dba()
        else:
            print("没有改商品，请重试")
            return dba()
    elif numb == '2':
        a = input("请输入你要修改的商品编号：")
        if a in commodity_list:
            b = input("请输入商品名称：")
            c = input("请输入商品价格：")
            commodity_list[a] = {'name':b,'price':c}
        else:
            print("你输入的商品编号不存在")
            return dba()
    elif numb == 'exit':
        return exit1()
register()
























































